package agents.task;

import java.util.ArrayList;
import java.util.List;

import agents.Ant;
import agents.resource.ResourceAgent;
import domain.TimeInterval;

//ant zal zich opsplitsen per interval
class EnterResourceAnt extends ExplorationAnt {
	//tijdsinterval wanneer resource binnen gegaan kan worden (wanneer vorige resource verlaten kan/moet worden) 
	private final TimeInterval enterTime;
	
	public EnterResourceAnt(ResourceAgent resource, TimeInterval enterTime, ExplorationAnt parentAnt) {
		super(resource, parentAnt);
		this.enterTime = enterTime;
	}
	
	@Override
	public void doStep() {
		List<TimeInterval> intervals = getCurrentResource().getFreeIntervals(enterTime.getStartTime(),getOrigin().getCar());
		List<Ant> children = new ArrayList<Ant>();
		
		for(TimeInterval i : intervals) {
			TimeInterval overlap = enterTime.getOverlapWith(i);
			if(overlap != null) {
				Trip newTrip = new Trip(getTrip());
				newTrip.append(new TripStep(getCurrentResource(),overlap.getStartTime()));
				children.add(new ExitResourceAnt(newTrip,this));
			}
		}
		
		setChildAnts(children);
		terminate();
	}
}
